package com.heima.schedule;

import org.redisson.Redisson;
import org.redisson.api.RLock;

public class TicketRunner implements Runnable{

    private int ticket=100;

    private Redisson redisson;

    public TicketRunner(Redisson redisson){
        this.redisson=redisson;
    }



    @Override
    public  void run() {

            while (true) {

                RLock mylock = redisson.getLock("mylock");
                boolean b = mylock.tryLock();
                if(b==true) {
                    try {
                        if (ticket > 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            ticket--;
                            System.out.println(Thread.currentThread().getName() + "还剩下" + ticket + "张票");

                        }
                    }catch(Exception ex){

                    }finally {
                        mylock.unlock();
                    }
                }
        }

    }
}
